package UMC.Web;


import UMC.Data.Utility;

import java.util.*;

public abstract class UIGridDialog extends UIDialog {

    private static class aGridDialog extends UIGridDialog {
        Header header;

        public aGridDialog(Header header, Object data) {
            this.asyncData(true);// = true;
            this.header = header;
            this.data = data;
        }

        Object data;


        @Override
        protected  Map<String,Object> getHeader() {
            return header.getHeaderMap();
        }

        @Override
        protected  Map<String,Object> getData(Map<String,Object>  paramsKey) {
            Map<String,Object> hash = new Hashtable<>();
            hash.put("data", data);

            return hash;
        }
    }


    public static UIGridDialog create(Header header, boolean isReturn, Object... data) {
        Object items = data;
        if (data.length == 1) {
            if (data[0].getClass().isArray()) {
                items = data[0];
            } else if (data[0] instanceof Collection) {
                items = data[0];
            }
        }
        aGridDialog aGridDialog = new aGridDialog(header, items);
        aGridDialog.IsReturnValue = isReturn;
        return aGridDialog;
    }


    public static class Header {
        Map<String,Object> headers =new HashMap<>();
        List<Map<String,Object>> fields = new LinkedList<>();

        public Header(int pageSize) {
            headers.put("type", "grid");
            headers.put("pageSize", pageSize);
        }

        public Header(String valueField, int pageSize) {
            headers.put("pageSize", pageSize);

            if (Utility.isEmpty(valueField)) {
                headers.put("type", "grid");
            } else {
                headers.put("ValueField", valueField);
                headers.put("type", "dialog");
            }
        }


        public Header add(String field, String name) {
            Map<String,Object> map = Utility.find(fields, f -> field.equals(f.get("Name")));
            if (map == null) {
                map = new HashMap<>();
                map.put("Name", field);
                map.put("type", "string");

                fields.add(map);
            }
            Map<String,String>  map1 = new HashMap<>();
            map1.put("text", name);
            map.put("config", map1);

            return this;
        }

        public  Map<String,Object> getHeaderMap() {

            headers.put("fields", fields);
            return headers;
        }


    }

    protected UIGridDialog() {
        this.IsReturnValue = true;
    }

    public boolean IsReturnValue;

    protected abstract  Map<String,Object> getHeader();

    protected abstract  Map<String,Object> getData( Map<String,Object> paramsKey);


    private boolean AutoSearch;

    @Override
    protected String type() {

        return "Grid";
    }

    private boolean IsSearch;

    private String Keyword;

    public void menu(String text, String model, String cmd, String value) {
        this.menu(new UIClick( model, cmd, value).text(text));
    }

    public void menu(UIClick... menus) {
        this.config.put("menu", menus);
    }

    public void menu(String text, String model, String cmd, WebMeta param) {
        this.menu(new UIClick( model, cmd, param).text(text));
    }


    private WebMeta ValueField;
    private boolean IsAsyncData;

    public UIGridDialog asyncData(boolean asyncData) {
        IsAsyncData = asyncData;
        return this;
    }

    public UIGridDialog returnValue(boolean returnValue) {
        IsReturnValue = returnValue;
        return this;
    }


    public UIGridDialog search(boolean search, boolean autoSearch) {
        IsSearch = search;
        AutoSearch = autoSearch;
        return this;
    }

    public UIGridDialog placeholder(String keyword) {
        Keyword = keyword;
        return this;
    }


    public UIGridDialog valueField(String... value) {
        this.ValueField = new WebMeta(value);
        return this;

    }


    @Override
    protected void initialization(WebContext context) {
        WebRequest request = context.request();
        WebResponse response = context.response();
        WebMeta items = request.sendValues();
        if (items != null && items.size() > 0) {
            response.redirect(this.getData(items.map()));
        }
        Map<String,Object> p = getHeader();
        if (this.ValueField != null) {
            p.put("ValueField", this.ValueField);
        }

        if (this.IsReturnValue) {
            p.put("type", "dialog");
        } else {
            p.put("type", "grid");
        }
        if (IsAsyncData) {
            this.config.put("Data", this.getData(new HashMap<>()));
        } else if (this.IsSearch) {
            p.put("search", Utility.isEmpty(this.Keyword) ? "搜索" : this.Keyword);
        }
        if (this.AutoSearch) {
            p.put("auto", true);
        }
        this.config.put("Header", p);

        super.initialization(context);
    }
}